package com.welsy.iter.command
{
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.welsy.iter.business.*;
	import com.welsy.iter.vo.*;
	import com.welsy.iter.event.GetIterEvent
	import com.welsy.iter.model.IterModelLocator;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.controls.Alert;
	import mx.collections.ICollectionView;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.utils.ArrayUtil;
	import com.welsy.iter.vo.iter.IterElementVO;

	
	/**
	 * @version	$Revision: 1.1 $
	 */
	public class GetIterCommand implements ICommand, IResponder
	{
	  	public function GetIterCommand()
		{	 
		}
	
		public function execute( event : CairngormEvent ): void
		{
			var getIterEvent:GetIterEvent = GetIterEvent( event );
			
			var delegate : GetIterDelegate = new GetIterDelegate( this );
			delegate.getIterComponents(getIterEvent.iterID);			    			
		}
	
		public function result( event : Object ) : void
		{				
			var icXML:XMLList = event.result as XMLList;
			var iterComponentsVO:IterComponentsVO = new IterComponentsVO;
			iterComponentsVO = parseXMLIterComponents(icXML);

			var model:IterModelLocator = IterModelLocator.getInstance();
			   		
			model.selectedIter = iterComponentsVO;								
		}
		
		private function parseXMLIterComponents(iclist:XMLList):IterComponentsVO
		{			
			var ic:IterComponentsVO = new IterComponentsVO;
				
			//get iter properties			
			var iterVO:IterVO = new IterVO;
			var iter:XML = XML(iclist.child(0).child(0));
			iterVO.setProperties(iter);			

			//set iter obj
			ic.iter = iterVO; 
							
			//get itercomponents properties					
			var item:XML;		
			var list:XMLList = iclist.descendants(IterQNames.qnIterElements).children();
									
            for each(item in list) {    	            		            		
            	var iterElement:IterElementVO = new IterElementVO();	            		            				            
            	iterElement.setProperties(item);
				
		    	//add iterElement to the itercomponents obj
                ic.addItem(iterElement);	  		            
		    }
			
			return ic;
		}
	
		public function fault( event : Object ) : void
		{
			var faultEvent : FaultEvent = FaultEvent( event );
			Alert.show( "Course could not be retrieved!" );
		}
	}

}